1712B - Woeful Permutation - CodeForces Solution


constructive algorithms greedy number theory

Please click on ads to support us..

Python Code:

def solve():
    n = int(input())
    a = [int(i + 1) for i in range(n)]
    for i in range(n - 2, -1, -2):
        a[i + 1], a[i] = a[i], a[i + 1]
    for i in range(n):
        print(a[i], end=' ')
    print()
    
if __name__ == '__main__':
    for i in range(int(input())):
        solve()

C++ Code:

#include <bits/stdc++.h>
using namespace std;
int main(){
    long long t,x,y,n;
    cin >> t;
    while (t--){
        cin >> n;
        if (n % 2){
            cout << 1 <<" ";
            for(int i = 2; i <= n; i++){
                if (i % 2 == 0) cout << i+1 <<" ";
                else cout << i-1 <<" ";  
            }
        }
        else {
            for (int i = 1; i <= n; i++){
                if(i % 2) cout << i+1 <<" ";
                else cout << i-1 << " ";
            }
        }   
        cout << endl;     
    }
}


Comments

Submit
0 Comments
More Questions

868A - Bark to Unlock
873B - Balanced Substring
1401D - Maximum Distributed Tree
1716C - Robot in a Hallway
1688B - Patchouli's Magical Talisman
99A - Help Far Away Kingdom
622B - The Time
1688C - Manipulating History
1169D - Good Triple
1675B - Make It Increasing
588A - Duff and Meat
1541B - Pleasant Pairs
1626B - Minor Reduction
1680A - Minimums and Maximums
1713A - Traveling Salesman Problem
1713B - Optimal Reduction
1710A - Color the Picture
1686B - Odd Subarrays
251A - Points on Line
427C - Checkposts
1159A - A pile of stones
508A - Pasha and Pixels
912A - Tricky Alchemy
1249A - Yet Another Dividing into Teams
1713C - Build Permutation
1699A - The Third Three Number Problem
1617B - GCD Problem
841A - Generous Kefa
1690B - Array Decrements
1692C - Where's the Bishop